﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Consul;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Recommend.API.Data;
using Recommend.API.Models;
using Microsoft.AspNetCore.Authentication.JwtBearer;

namespace Recommend.API
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var myConn = Configuration.GetConnectionString("default");
            services.AddDbContext<ProjectRecommendContext>(opt =>
            {
                opt.UseMySQL(myConn);
            });
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
              .AddJwtBearer(opt =>
              {
                  opt.Audience = "recommend_api";
                  opt.Authority = "http://localhost:8000";
                  opt.RequireHttpsMetadata = false;
                  opt.SaveToken = true;
              });
            services.AddOptions();
            services.Configure<ServiceDiscoveryOptions>(Configuration.GetSection("ServiceDiscovery"));
            services.AddSingleton<IConsulClient>(p => new ConsulClient(cfg =>
            {
                var serviceConfiguration = p.GetRequiredService<IOptions<ServiceDiscoveryOptions>>().Value;

                if (!string.IsNullOrEmpty(serviceConfiguration.Consul.HttpEndpoint))
                {
                    // if not configured, the client will use the default value "127.0.0.1:8500"
                    cfg.Address = new Uri(serviceConfiguration.Consul.HttpEndpoint);
                }
            }));
            services.AddCap(opt =>
            {
                //opt.UseEntityFramework<ApplicationDbContext>();
                //opt.UseRabbitMQ("140.143.88.100");
                //opt.UseDashboard();
                //opt.UseDiscovery();
                opt.UseEntityFramework<ProjectRecommendContext>()
                //.UseMySql(Configuration.GetConnectionString("default"))
                .UseRabbitMQ("140.143.88.100")
                .UseDashboard()
                .UseDiscovery(discoveryOpt =>
                {
                    discoveryOpt.DiscoveryServerHostName = "localhost";
                    discoveryOpt.DiscoveryServerPort = 8500;
                    discoveryOpt.CurrentNodeHostName = "localhost";
                    discoveryOpt.CurrentNodePort = 8007;
                    discoveryOpt.NodeName = "Cap Recommend node";
                    discoveryOpt.NodeId = 1;
                });
            });
            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseCap();
            app.UseMvc();
        }
    }
}
